<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');

class RestaurantsControllerComments extends JController
{
	function save()
	{
		JRequest::checkToken() or jexit( 'Invalid Token' );
		
		global $option;
		
		$row =& JTable::getInstance('comment', 'Table');

		if (!$row->bind(JRequest::get('post'))) 
		{
			JError::raiseError(500, $row->getError() );
		}
		
		if (!$row->store()) 
		{
			JError::raiseError(500, $row->getError() );
		}
		
		$this->setRedirect('index.php?option=' . $option . '&controller=comments', 'Comment Saved');
	}
	
	function remove()
	{
		JRequest::checkToken() or jexit( 'Invalid Token' );
		
		global $option;
		
		$cid = JRequest::getVar('cid', array(0));
		
		$row =& JTable::getInstance('comment', 'Table');
		
		foreach ($cid as $id) {
			$id = (int) $id;
			
			if (!$row->delete($id)) {
				JError::raiseError(500, $row->getError() );
			}
		}
		
		$s = '';
		
		if (count($cid) > 1) {
			$s = 's';
		}
		
		$this->setRedirect('index.php?option=' . $option . '&controller=comments', 'Comment' . $s . ' deleted.');
	}
	
	function display()
	{
		$view = JRequest::getVar('view');
		
		if (!$view) {
			switch ($this->getTask()) {
				case 'edit':
					JRequest::setVar('view', 'comment');
					break;
				
				default:
					JRequest::setVar('view', 'comments');
					break;
			}
		}

		parent::display();
	}
}
